#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include <ESP8266HTTPClient.h>



#define WATCHDOG_TIMEOUT 30000
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT

#include <Blinker.h>

char auth[] = "1a186d1f3d9f";
char ssid[] = "zhouliangan44";
char pswd[] = "wxg13948532823";
// char ssid[] = "AQM";
// char pswd[] = "12345678";

String URL_living = "http://192.168.124.2/Livingroom";
String URL_bed = "http://192.168.124.2/Bedroom";
String URL_yard = "http://192.168.124.2/Yard";

#define Sw1 D0 // 外间
#define Sw2 D1 // 里间
#define Sw3 D2 // 院灯
#define Sw4 D3 // 总控

unsigned long count_time_ya;
unsigned long count_time_living;
unsigned long count_time_bed;
unsigned long count_time_ya_cash;
unsigned long count_time_living_cash;
unsigned long count_time_bed_cash;

bool sb1, sb2, sb3;
// bool li1, li2, li3;
ESP8266WebServer esp8266_server(80); // 建立ESP8266WebServer对象，该对象用于响应HTTP请求。监听端口（80）
WiFiClient wifiClient;

#include "wifi.h"
#include "xiaoai.h"

void setup()
{
  pinMode(Sw1, INPUT);
  pinMode(Sw2, INPUT);
  pinMode(Sw3, INPUT);
  pinMode(Sw4, INPUT);

  sb1 = digitalRead(Sw1); // 读取按钮状态
  sb2 = digitalRead(Sw2); // 读取按钮状态
  sb3 = digitalRead(Sw3); // 读取按钮状态

  Serial.begin(115200);
  //initWeb();
  blinker_setup();
}
void loop()
{
  Blinker.run();
  detactswitch();
  if (Serial.available())
  {
    uint8_t i = Serial.readString().toInt();
    httpClientRequest(i);
  }
}

void detactswitch()
{
  bool b1 = digitalRead(Sw1); // 读取按钮状态
  bool b2 = digitalRead(Sw2); // 读取按钮状态
  bool b3 = digitalRead(Sw3); // 读取按钮状态
  if (sb1 != b1)
  {
    delay(100);
    if (sb1 != b1)
    {
      httpClientRequest(1);
      sb1 = b1;
    }
  }
  if (sb2 != b2)
  {
    delay(100);
    if (sb2 != b2)
    {
      httpClientRequest(2);
      sb2 = b2;
    }
  }
  if (sb3 != b3)
  {
    delay(100);
    if (sb3 != b3)
    {
      httpClientRequest(3);
      sb3 = b3;
    }
  }
}